package top.ninwoo.bishe.starter.service;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import top.ninwoo.bishe.starter.config.ClientProperties;
import top.ninwoo.common.entity.NetworkTopology;
import top.ninwoo.common.entity.SeparatedClusterConfig;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ClusterServiceImpl implements ClusterService {

    private final static String SEND_CLUSTER_CONFIG_TO_EDGE_NODE
            = "/cluster/sendClusterConfigToEdgeNode";
    private final static String REMOVE_CLUSTER_FROM_EDGE_NODE
            = "/cluster/removeClusterFromEdgeNode?clusterId=";
    private final static String GET_ALL_EDGE_NODE_IDS
            = "/cluster/getEdgeNodeIds";
    private final static String ADJUST_CLUSTER_TO_EDGE_NODE
            = "/cluster/adjustCluster";
    private final static String SEND_LOGIC_TOPO
            = "/cluster/sendLogicTopo";
    private final static String ADJUST_LOGIC_TOPO
            = "/cluster/adjustLogicTopo";

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private ClientProperties clientProperties;

    @Override
    public List<SeparatedClusterConfig> sendClusterConfigToEdgeNode(List<SeparatedClusterConfig> configs) {
        Map<String, Object> param = new HashMap<>();

        param.put("configs", configs.toArray());

        List<SeparatedClusterConfig> results
                = restTemplate.postForObject("http://" + clientProperties.getCloudUrl() + SEND_CLUSTER_CONFIG_TO_EDGE_NODE, configs, List.class);
        return results;
    }

    @Override
    public String removeClusterFromEdgeNode(Long clusterId) {
        return restTemplate.getForObject("http://" + clientProperties.getCloudUrl() +
                REMOVE_CLUSTER_FROM_EDGE_NODE + clusterId, String.class);
    }

    @Override
    public List<String> getAllEdgeNodeIds() {
        return restTemplate.getForObject("http://" + clientProperties.getCloudUrl() +
                GET_ALL_EDGE_NODE_IDS, List.class);
    }

    @Override
    public boolean adjustClusterToEdgeNode(List<SeparatedClusterConfig> configs) {
        Map<String, Object> param = new HashMap<>();

        param.put("configs", configs.toArray());

        ResponseEntity<Boolean> result = restTemplate.postForEntity("http://" + clientProperties.getCloudUrl() + ADJUST_CLUSTER_TO_EDGE_NODE, configs, Boolean.class);

        return result.getStatusCode().is2xxSuccessful();
    }

    @Override
    public boolean sendLogicTopoToEdgeNode(List<SeparatedClusterConfig> configs) {
        Map<String, Object> param = new HashMap<>();

        param.put("configs", configs.toArray());

        ResponseEntity<Boolean> result = restTemplate.postForEntity("http://" + clientProperties.getCloudUrl() + SEND_LOGIC_TOPO, configs, Boolean.class);

        return result.getStatusCode().is2xxSuccessful();
    }

    @Override
    public boolean adjustLogicTopoToEdgeNode(List<SeparatedClusterConfig> configs) {
        Map<String, Object> param = new HashMap<>();

        param.put("configs", configs.toArray());

        ResponseEntity<Boolean> result = restTemplate.postForEntity("http://" + clientProperties.getCloudUrl() + ADJUST_LOGIC_TOPO, configs, Boolean.class);

        return result.getStatusCode().is2xxSuccessful();
    }


}
